﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI.WebControls;
using Medianamik.Core.Configuration;
using Medianamik.Core.Profile;
using Medianamik.Core;
using Medianamik.Globalization;

namespace Medianamik.UI.Web
{
    public partial class UserPreferences : BasePage
    {
        readonly static MedianamikConfigSection Config = (MedianamikConfigSection)ConfigurationManager
            .GetSection(ConfigurationSectionName.MEDIANAMIK);

        public string ReturnUrl
        {
            get
            {
                return Request[DefaultParamNames.ReturnURL] as string ?? "/Medianamik";  
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindControls();
                MultiLanguageFeatures.Visible = Config.EnableMultiLanguageFeatures;
            }
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
               var profile = (UserProfile)HttpContext.Current.Profile;
               int toolbarMode;
                if(int.TryParse(rblToolbarMode.SelectedValue, out toolbarMode ))
                {
                    profile.EditorToolbarMode = toolbarMode;
                }

                if (rblCultureName.SelectedItem != null)
                {
                    profile.UICultureName = rblCultureName.SelectedValue;
                }

                profile.EnableMultiLanguageFeatures = Boolean.Parse(MultiLanguageFeaturesOptions.SelectedValue);

                profile.MaySpecifyMedianamikLinkProtocol = Boolean.Parse(MedianamikLinkProtocolChoice.SelectedValue);

                Response.Redirect(ReturnUrl);
            }
        }

        private void BindControls()
        {
            var profile = (UserProfile)HttpContext.Current.Profile;
            //CultureName

            foreach (var culture in ConfigManager.GetMedianamikCultures())
            {
                rblCultureName.Items.Add(new ListItem(culture.CultureInfo.DisplayName, culture.CultureName));
            }
            var selectedItem = rblCultureName.Items.FindByValue(profile.UICultureName);
            if (selectedItem != null)
            {
                selectedItem.Selected = true;
            }


            //ToolbarMode
            var toolbarMode = profile.EditorToolbarMode;
            rblToolbarMode.Items.Add(new ListItem(Translator.Translate("Medianamik_Global","Preferences_Default" ), ((int)Telerik.Web.UI.EditorToolbarMode.Default).ToString()));
            rblToolbarMode.Items.Add(new ListItem(Translator.Translate("Medianamik_Global", "Preferences_ShowOnFocus"), ((int)Telerik.Web.UI.EditorToolbarMode.ShowOnFocus).ToString()));
            rblToolbarMode.Items.Add(new ListItem(Translator.Translate("Medianamik_Global", "Preferences_PageTop"), ((int)Telerik.Web.UI.EditorToolbarMode.PageTop).ToString()));
            rblToolbarMode.Items.Add(new ListItem(Translator.Translate("Medianamik_Global", "Preferences_Floating"), ((int)Telerik.Web.UI.EditorToolbarMode.Floating).ToString()));

            selectedItem = rblToolbarMode.Items.FindByValue(toolbarMode.ToString());
            if (selectedItem != null)
            {
                selectedItem.Selected = true;
            }

            //MultiLanguageFeatures
            MultiLanguageFeaturesOptions.SelectedValue = profile.EnableMultiLanguageFeatures.ToString();

            MedianamikLinkProtocolChoice.SelectedValue = profile.MaySpecifyMedianamikLinkProtocol.ToString();
        }
    }
}